$("#supplier").bootstrapTable({
    url: "/supplier/list",
    method: "get",
    pagination: true,   //是否显示分页（*）
    pageSize: 10,       //每页的记录行数（*）
    pageList: [10, 20, 50, 100, 'All'], //可供选择的每页的行数（*）
    sidePagination: "client",    //分页方式：client客户端分页，server服务端分页（*）
    pageNumber: 1,                      //初始化加载第一页，默认第一页
    classes: "table table-bordered table-striped table-condensed table-hover",                  //初始化加载第一页，默认第一页
    columns: [{
        title: "id",
        field: "id",
    }, {
        title: "供应商名称",
        field: "supplierName",
    }, {
        title: "备注",
        field: "note",
    }, {
        field: 'operate',
        title: '操作',
        align: 'center',
        valign: 'middle',
        width: 200,
        events: {
            'click #edit': function (e, value, row, index) {
                $('#update-id').val(row.id);
                $('#update-supplierName').val(row.supplierName);
                $('#update-note').val(row.note);

            },
            'click #delete': function (e, value, row, index) {
                $('#delete-id').val(row.id);
                $('#delete-supplierName').val(row.supplierName);
                $('#delete-note').val(row.note);
            }
        },
        formatter: function (value, row, index) {
            var result = "";
            result += '<button id="edit" class="btn btn-info" data-toggle="modal" data-target="#updateModal">编辑</button>';
            result += '<button id="delete" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal" style="margin-left:10px;">删除</button>';
            return result;
        }
    }]
});

function searchSupplierOnClick() {
    $.get({
        url: "/supplier/list",
        data: {
            supplierName: $("#search-supplierName").val()
        },
        dataType: "json",
        success: function (json) {
            $("#supplier").bootstrapTable('load', json);
        }
    });
}

//按下Enter查询
$(document).keyup(function (event) {
    if (event.keyCode == 13){
        searchSupplierOnClick();
    }

});

$("#reset").click(function () {
    $("#search-supplierName").val("");
})

$("#addSupplier").click(function () {
    $.post({
        url: "/supplier/addOne",
        async: false,//同步，默认是true，异步
        data: {
            supplierName: $("#add-supplierName").val(),
            note: $("#add-note").val()
        },
        success: function (data) {
            let result = data;
            layui.use('layer', function () {
                let layer = layui.layer;
                if (result.success) {
                    layer.msg("操作成功，成功行数:   " + result.data, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            });
        }
    });
    $("#addModal").modal('hide');
    searchSupplierOnClick();
});

$("#updateSupplier").click(function () {
    $.post({
        url: "/supplier/update",
        async: false,//同步，默认是true，异步
        data: {
            id: $("#update-id").val(),
            supplierName: $("#update-supplierName").val(),
            note: $("#update-note").val()
        },
        success: function (data) {
            let result = data;
            layui.use('layer', function () {
                let layer = layui.layer;
                if (result.success) {
                    layer.msg("操作成功，成功行数:   " + result.data, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            });
        }
    });
    $("#updateModal").modal('hide');
    searchSupplierOnClick();
});

$("#deleteSupplier").click(function () {
    $.post({
        url: "/supplier/delete",
        async: false,//同步，默认是true，异步
        data: {
            id: $("#delete-id").val()
        },
        success: function (data) {
            let result = data;
            layui.use('layer', function () {
                let layer = layui.layer;
                if (result.success) {
                    layer.msg("操作成功，成功行数:   " + result.data, {icon: 1});
                } else {
                    layer.msg(result.msg, {icon: 2});
                }
            });
        }
    });
    $("#deleteModal").modal('hide');
    searchSupplierOnClick();
});
